#define VERSION "0.1"
#include <stdio.h>
#include "./arg.h"

int main(int argc, char *argv[]) {
	char name[20];	/* Used to store the name to say hello to */
	int pos;		/* Used to hold return values of argswitch() */

	if (argswitch(argc,argv,"--usage")) {	/* if "usage" switch is present */
		printf("ShiftOut Hello World, Version %s\n",VERSION);	/* display usage info */
		printf("Usage:\n");
		printf("\t%s [--usage|--name name|--file filename]\n",argv[0]);
		printf("\n\nSee man hello(1) for more details.\n");
		return(0);
	} else if (pos = argswitch(argc,argv,"--name")) {	/* if name is specified on command line */
		strcpy(name,argv[(pos+1)]);				/* use name from command line */
	} else if (pos = argswitch(argc,argv,"--file")) {	/* if name is stored in file */
		FILE *fp;	/* file pointer */
		int c;	/* counter */

		if(fp = fopen(argv[(pos+1)],"r")) {
			name[19] = '\0'; /* make last charachter in array NULL in case more than 19 charachters in plain-text file */
						/*TODO: detect if too many charachters in plain-text file and produce error code
							and message on stderr */
			for(c = 0; c < 19; ++c) {
				name[c] = getc(fp);	/* get next charachter from file */
				if (name[c] == EOF) {	/* if end of file */
					name[c] = '\0';	/* make characher NULL to show end of string */
					break;		/* escape for loop */
				}
			}
		}
	} else { /* if no options given */
		strcpy(name,"World");	/* just say hello to World */
	}

	printf("Hello %s.\n",name); /* give output on stdout */
	return(0);	/* exit cleanly */
}
